import 'package:json_annotation/json_annotation.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

part 'c_bean_good_item.g.dart';

/*
用于组件数据显示, ui显示数据
 */
@JsonSerializable(explicitToJson: true,includeIfNull: false)
class ComponentBeanGoodItem {


  String? productType;
  String? productTypeName;

  String? bomId;
  String? itemCode; //名称
  String? itemId; //名称
  String? itemName; //名称
  String? specName; //名称
  String? specId; //名称
  String? mcId; // mcId
  dynamic ruleType;
  dynamic specType;
  dynamic bomType;
  dynamic isBom; //1 是否bom
  dynamic batchType; //1 是否批次
  dynamic isBatch; //1 是否批次
  dynamic hasLabel; //1 是否标签商品
  dynamic isUniqueCode; //api有时是String,有时是int "1"  判断时统一转字符串判断
  dynamic uniqueCodeType; //唯一码管理类型，0出入库，1仅出库
  bool userTagIsContainZero=false;//数量为0是否保留

  dynamic invQty; //库存


  String? orderLineItemId; // 商品行ID
  String? remark;
  dynamic validDays;//有效期

  String? unit; //单位
  dynamic itemQuantity; //单位数量
  String? itemUnit; //单位
  // String? unitName; //单位
  dynamic isUnpacking;
  dynamic quantity; //数量
  dynamic reQuantity;// 已退数量
  dynamic refundQuantity; //退货数量
  dynamic shoutongQuantity; //收桶数量
  dynamic treatMaterialsQuantity;// 待发数量
  dynamic alreadyMaterialsQuantity;// 已发数量
  dynamic unPickQuantity;// 待拣配数量
  dynamic putQuantity;// 已投数量
  dynamic planQuantity;// 计划数量
  dynamic deliveryQuantity;
  dynamic itemUnitQuantity;
  dynamic unitRate;
  dynamic returnQty;
  dynamic pickQuantity;
  dynamic minPrice;
  dynamic maxPrice;

  Map<String,dynamic>? attachedData;
  List<dynamic>? attributesList;
  Map<String,dynamic>? mitemDisassemblyData;

  Map<String,dynamic>? fromRelocateData;
  Map<String,dynamic>? toRelocateData;

  Map<String,dynamic>? priceTagData;  //商品价签设置

  String get getUnit {
    return unit == null ? '' : unit!.length < 4 ? unit! : '${unit!.substring(0,4)}..';
  }

  String get getItemUnit {
    return itemUnit == null ? '' : itemUnit!.length < 4 ? itemUnit! : '${itemUnit!.substring(0,4)}..';
  }

  dynamic totalQuantity; //总数量
  dynamic totalMoney; //总金额
  dynamic totalInvQty; // 总库存

  bool? isUserSelect;
  bool isShowSpecList = true;
  bool isUnfold = false; // 是否展开收起

  dynamic unitInQuantity;
  dynamic unitOutQuantity;
  dynamic bomQuantity;
  String? bomUnitName;
  dynamic bomUnitRate;

  String? orderItemId;

  //添加多规格商品界面使用
  List<dynamic>? temp_goodSpec12SelectList; // 规格12选择列表数据 Bean_good_spec_1
  String? temp_currentSpecUnit; //当前选择单位
  double? temp_currentSpecUnitRate; //当前选择单位
  List<ComponentBeanUnit>? temp_currentSpecUnitList;//当前规格可选单位
  String? temp_currentSpecImageUrl; //当前规格图片
  dynamic temp_currentSpecQuantity; //当前规格已选数量
  dynamic temp_currentSpecId; //当前规格id
  dynamic temp_currentSpecName; //当前规格

  Map<String,dynamic>? temp_map_data; //通用临时信息
  Map<String,dynamic>? itemShelves;
  //溯源码流程
  String? regularSku;
  String? regularUnique;//唯一码
  dynamic regularQuantity;//对应的数量
  String? regularFullCode;//溯源码



  // // 条码管理
  // String? barCode;
  // dynamic isAvailableForSale; // 是否可售
  // bool? isOnHand; // 是否在库


  //店内加工
  String? adjustType;
  List<dynamic>? adjustTypeList;

  //多单位
  List<ComponentBeanUnit>? unitList;
  //已选规格列表数据(数据表实体类)
  List<ComponentBeanGoodItem>? currentSpecList;
  //唯一码
  List<ComponentBeanUniqueCode>? currentUniqueCodeList;
  //批次码
  List<ComponentBeanBatchCode>? currentBatchCodeList;
  // 货架列表
  List<dynamic>? shelves;

  //bom
  List<ComponentBeanGoodItem>? currentBomList;
  bool? isShowBomList;

  double? currentPrice; //当前价格(可能不启用,则不需要赋值)

  String? currentUnitId;
  String? currentUnit; //当前选中单位
  double? currentUnitRate;


  String? author;
  String? pubTime;

  @JsonKey(readValue: _readValueToDealPrice)
  double? dealPrice; //价格
  static double? _readValueToDealPrice(Map<dynamic,dynamic> json,String key){
    return XNumberUtils.dynamic2double(json[key]??json['salePrice']);
  }

  @JsonKey(readValue: _readValueSkuBarcode)
  String? skuBarcode;
  static String? _readValueSkuBarcode(Map<dynamic,dynamic> json,String key){
    return json[key]??json['skubarcode'];
  }

  String? imageUrl; //图片
  String? headImage; //图片

  @JsonKey(readValue: _readValueImageUrlFull)
  String? imageUrlFull; //图片
  static String? _readValueImageUrlFull(Map<dynamic,dynamic> json,String key){
    return json[key]??json['imageUrl']??json['imgUrl'];
  }

  @JsonKey(readValue: _readValueJsonData)
  dynamic jsonData;  //  通常是api返回的原始整个item map
  static dynamic _readValueJsonData(Map<dynamic,dynamic> json,String key){
    if(json[key] is String){
      return XJsonUtils.json2Map(json[key]);
    }
    return json[key];
  }

  double? maxAvailableQuantity; // 最大可选数量

  double currentQuantity = 0.0; // 本地已选数量
  double currentBaseQuantity = 0.0; // 本地已选数量 多规格时，此数量应该为总数量
  double currentMoney = 0.0; // 如果是多规格时，此数量应该为总金额

  dynamic settlementPrice;//代发结算单价
  dynamic baseDeQuantity;//已发数量
  dynamic realPrice;//已发数量
  dynamic costPrice;//成本价
  dynamic totalCostPrice;//成本总额
  dynamic saleUnitPrice;
  dynamic listUnitPrice;
  dynamic dealUnitPrice;
  dynamic packageItemQuantity;
  dynamic mentityCardUnifiedPrice;
  bool? canEditItemPrice;
  bool? groupCouponItem; //
  dynamic packageUnitPrice;
  dynamic discouAmount;
  dynamic hasLabelGroup;
  List<Map<String, dynamic>>? labelGroups;
  //零售
  dynamic itemKey;
  String? mainSpecId;
  dynamic giftMustQty;
  dynamic requireQtyInSuit;
  dynamic isControlBuy;
  dynamic shopCartItemType;
  List<ComponentBeanGoodItem>? productSet;
  List<ComponentBeanGoodItem>? comboItemList;
  List<ComponentBeanGoodItem>? bomProductSet;
  List<ComponentBeanGoodItem>? giftProductSet;
  List<ComponentBeanGoodItem>? additionalItemProductSet;
  List<ComponentBeanBatchCode>? batchList;

  ComponentBeanGoodItem({
    this.productType,
    this.productTypeName,
    this.specType,
    this.bomId,
    this.itemId,
    this.specId,
    this.itemName,
    this.mcId,
    this.specName,
    this.remark,
    this.temp_currentSpecQuantity = 0.0,
    this.currentQuantity = 0.0,
    this.currentBaseQuantity = 0.0,
    this.maxAvailableQuantity,
    this.currentMoney = 0.0,
    this.dealPrice,
    this.invQty,
    this.currentPrice,
    this.currentUnitRate,
    this.bomType,
    this.isBom,
    this.isShowSpecList = true,
    this.isUniqueCode,
    this.uniqueCodeType,
    this.batchType,
    this.isBatch,
    this.hasLabel,
    this.userTagIsContainZero = false,
    this.currentSpecList,
    this.currentBomList,
    this.temp_goodSpec12SelectList,
    this.currentUniqueCodeList,
    this.currentUnit,
    this.unit,
    // this.unitName,
    this.temp_map_data,
    this.priceTagData,
    this.itemShelves,
    this.isUnfold = false,
    this.adjustType,
    this.adjustTypeList,
    this.attachedData,
    this.attributesList,
    this.fromRelocateData,
    this.toRelocateData,
    this.mitemDisassemblyData,
    this.costPrice,
    this.totalCostPrice,
    this.bomQuantity,
    this.bomUnitName,
    this.bomUnitRate,
    this.jsonData,
  });

  factory ComponentBeanGoodItem.fromJson(Map<String, dynamic> json) =>
      _$ComponentBeanGoodItemFromJson(json);

  Map<String, dynamic> toJson() => _$ComponentBeanGoodItemToJson(this);
}


//单位
@JsonSerializable(explicitToJson: true,includeIfNull: false)
class ComponentBeanUnit {

  String? unitName;
  String? unit;//单据详情使用
  String? unitId;
  String? specId;

  @JsonKey(readValue: _readValueToRate)
  double? unitRate;

  static double _readValueToRate(Map<dynamic,dynamic> json,String key){
    double rate =XNumberUtils.dynamic2double(json[key])??1.0;
    if(rate==0){
      rate=1.0;
    }
    return rate;
  }

  dynamic itemUnitPrice;  // 单位价格
  dynamic expressFee;
  dynamic isOrder;//默认开启

  ComponentBeanUnit();

  factory ComponentBeanUnit.fromJson(Map<String, dynamic> json) => _$ComponentBeanUnitFromJson(json);

  Map<String, dynamic> toJson() => _$ComponentBeanUnitToJson(this);
}

//唯一码
@JsonSerializable(explicitToJson: true,includeIfNull: false)
class ComponentBeanUniqueCode {


  String? uniqueCode;
  String? attachBarcode;
  String? barcode;
  String? specId;
  bool? mentityCardUnifiedPrice;
  dynamic mentityCardsResult; // 实体卡信息
  //属性
  List<dynamic>? attrs;
  String? remark;
  double? costPrice;

  dynamic isOpenUniqueCodeAttr;
  dynamic minSalePrice;

  ComponentBeanUniqueCode(
      {
        this.uniqueCode,
        this.attachBarcode,
        this.remark,
      }
      );


  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is ComponentBeanUniqueCode &&
              runtimeType == other.runtimeType &&
              uniqueCode == other.uniqueCode;

  @override
  int get hashCode => uniqueCode.hashCode;

  factory ComponentBeanUniqueCode.fromJson(Map<String, dynamic> json) =>
      _$ComponentBeanUniqueCodeFromJson(json);

  Map<String, dynamic> toJson() => _$ComponentBeanUniqueCodeToJson(this);


}

//批次码
@JsonSerializable(explicitToJson: true,includeIfNull: false)
class ComponentBeanBatchCode {

  String? specId;
  String? docItemId;
  String? batchCode;
  dynamic productionDate;
  dynamic validDays;
  //api有时候是库存数,有时候是购物车数,注意区分
  dynamic quantity;

  double currentQuantity=0.0;

  //唯一码和唯一码的属性
  List<ComponentBeanUniqueCode>? barcodeList;


  ComponentBeanBatchCode(
      {
        this.currentQuantity=0.0,
        this.batchCode,
        this.barcodeList,
        this.validDays,
        this.quantity,
        this.productionDate,
      }
      );


  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is ComponentBeanBatchCode &&
          runtimeType == other.runtimeType &&
          batchCode == other.batchCode;

  @override
  int get hashCode => batchCode.hashCode;

  factory ComponentBeanBatchCode.fromJson(Map<String, dynamic> json) =>
      _$ComponentBeanBatchCodeFromJson(json);

  Map<String, dynamic> toJson() => _$ComponentBeanBatchCodeToJson(this);


}